package com.ym.medical.service.impl;

import com.ym.common.constant.Constants;
import com.ym.core.exception.BusinessException;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.ConsultSummaryDao;
import com.ym.medical.domain.bo.PublishCouponTarget;
import com.ym.medical.domain.entity.ConsultSummaryEntity;
import com.ym.medical.domain.entity.CounsellingRoomEntity;
import com.ym.medical.domain.entity.InquiryOrderEntity;
import com.ym.medical.domain.param.ConsultSummaryParam;
import com.ym.medical.domain.vo.ConsultSummaryVo;
import com.ym.medical.enums.CounsellingRoomStateEnum;
import com.ym.medical.enums.CouponSendWayEnum;
import com.ym.medical.service.IConsultSummarySV;
import com.ym.medical.service.ICounsellingRoomSV;
import com.ym.medical.service.ICouponReceiveRecordSV;
import com.ym.medical.service.IInformationSV;
import com.ym.medical.service.IInquiryOrderSV;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * ComputerName:LAPTOP-6GI4HOI6
 * Author:625
 * Date: 2019-05-17
 * Time: 13:49:25
 * 服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ConsultSummarySVImpl extends BaseSVImpl<ConsultSummaryEntity, ConsultSummaryDao> implements IConsultSummarySV {

	private static final Logger LOGGER = LoggerFactory.getLogger(ConsultSummarySVImpl.class);

	@Autowired
	private ICounsellingRoomSV counsellingRoomSV;

	@Autowired
	private IInformationSV informationSV;

	@Autowired
	private IInquiryOrderSV inquiryOrderSV;

	@Autowired
	private ICouponReceiveRecordSV couponReceiveRecordSV;

	@Override
	public ConsultSummaryVo selectSummaryByCourId(Long courId) {
		return mapper.selectConsultSummaryModel(courId);
	}

	@Override
	public boolean summary(ConsultSummaryParam consultSummary, Long doctId) {
		long countNum = this.countByParams(ConsultSummaryEntity.builder().courId(consultSummary.getCourId()).build());
		if (countNum > 0) {
			return true;
		}
		//1. 医生总结
		ConsultSummaryEntity covert = consultSummary.covert();
		this.insert(covert);
		LOGGER.info("医生总结成功 总结id:{}" + covert.getId());

		//2.修改聊天室状态
		Long courId = consultSummary.getCourId();
		CounsellingRoomEntity counsellingRoom = counsellingRoomSV.selectById(courId);
		if (counsellingRoom == null) {
			throw new BusinessException("医生总结异常 cause:(聊天室为空)");
		}
		counsellingRoom.setCourState(CounsellingRoomStateEnum.HAVE_SUMMARIZED.getKey());
		counsellingRoomSV.updateById(counsellingRoom);
		LOGGER.info("修改聊天室状态为 聊天室id:{}" + counsellingRoom.getId());
		//3.构建医生总结消息并发送
		informationSV.generateInquiryInformation(counsellingRoom.getIoId(), Constants.InquiryAdvice.SUMMARY);
		//4.发放优惠劵给患者
		Long couponId = consultSummary.getCouponId();
		InquiryOrderEntity order = inquiryOrderSV.selectById(counsellingRoom.getIoId());
		if (order != null && couponId != null) {
			PublishCouponTarget couponTarget = new PublishCouponTarget();
			couponTarget.setPatientId(order.getPatientId());
			couponTarget.setUserId(order.getUserId());
			couponReceiveRecordSV.publishCoupon(
					couponId, CouponSendWayEnum.CONCLUSION.getKey(), consultSummary.getId(), doctId, null, couponTarget);
		}
		return true;
	}


}
